package com.chapp.lastFmWs.parser.base;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

public abstract class ResponseParser {
	// <lfm status="failed">
	// <error code="10">Invalid API Key</error>
	// </lfm>

	private final String tag = getClass().getName();

	protected RootElement rootLmf = new RootElement("lfm");

	protected boolean statusOk = false;
	protected String error;

	protected void parseXml(String xml) throws SAXException {
		Xml.parse(xml, this.rootLmf.getContentHandler());
	}

	public ResponseParser() {
		rootLmf.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				if (attributes.getValue(0).equals("ok")) {
					statusOk = true;
				} else {
					setErrorListener();
				}
			}
		});
	}

	private void setErrorListener() {
		rootLmf.requireChild("error").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						error = body;
					}
				});
	}
}
